<?php

class event_details
{
	var $mode;
	var $id;
	
	var $event;
	var $dates = Array();
	
	var $items = Array();

	function event_details($event, $mode)
	{
		$this->event = $event;
		$this->id = $event['e_id'];
		$this->mode = $mode;
		//get event dates
		$this->setup_data();
		
		if($mode == 'overview')
		{
			$this->setup_overview();
		}
			
	}
	
	function render()
	{
		foreach ($this->items as $item)
		{
			$item->render();
		}
		
		if($this->mode == 'Mini')
		{
			$this->renderMini();
		}
		else if($this->mode == 'MiniSolo')
		{
			$this->renderMini(true);
		}
		else if($this->mode == 'overview')
		{
			global $skin_dir;
			
			$this->renderOverview();
		}
	}
	
	function renderMini($solo = false)
	{
		global $lang, $skin_dir;
                
                $this->event = formatArrayOutput($this->event);
		
		if($this->event['t_logo'] == '')
		{
			$logo = '';
		}
		else
		{
			$logo = '<a href="events.php?id=' . $this->event['e_id'] . '"><img src="' . $this->event['t_logo'] . '" alt=""></a>';
		}
		
		
		$champlogo = '';
		$licenselogo = '';
                $gamelogo = '';
		
		if($solo)
		{//
			$title = '<a href="events.php?id=' . $this->event['e_id'] . '">' .
                                       $this->event['c_title'] . ': ' . $this->event['e_title'] .'</a>';

			if($this->event['c_logoThmbURL'] != '')
			{
				$champlogo = '<a href="championships.php?id=' . $this->event['c_id'] . '"><img class="image30pxhigh" align="middle" src="' . $this->event['c_logoThmbURL'] . '" alt=""></a>';
			}
			if($this->event['g_logoThmb'] != '')
			{
				$gamelogo = '<img class="image30pxhigh" align="middle" src="' . $this->event['g_logoThmb'] . '" alt="">';
			}
                        if($this->event['l_imageThmb'] != '' && isset($_SESSION['valid_id']))
                        {
                                $licenselogo = '<a href="drivers.php?id=' . $_SESSION['valid_id'] . '&mode=license"><img class="image30pxhigh" align="middle" src="' . $this->event['l_imageThmb'] . '"/></a>';
                        }
                        else if($this->event['l_imageThmb'] != '')
                        {
                                $licenselogo = '<img class="image30pxhigh" align="middle" src="' . $this->event['l_imageThmb'] . '"/>';
                        }
		
		}
		else
		{
			$title = '<a href="events.php?id=' . $this->event['e_id'] . '">' .  $this->event['e_title'] . ' : ' .
							 $this->event['t_fullTitle'] . '</a>';	
		}
		
		$signups = '';
                if($this->event['e_openSignUps'] == 1)
                {
                    //not logged in
                    if(! isset($_SESSION['valid_id']))
                        $signups = '<a href="index.php?action=login">' . sprintf($lang['LoginToEnter'], $lang['IncidentEvent']) . '</a>';

                    //already entered event
                    else if($_SESSION['valid_id'] == $this->event['evententered'])
                        $signups = '<a href="events.php?mode=entrants&id=' . $this->event['e_id'] . '">' .
                                sprintf($lang['AlreadyEntered'], $lang['IncidentEvent']) . '</a>';

                    //incorrect license
                    else if($this->event['l_order'] < $this->event['userlicense'])
                        $signups = '<a class="closed" href="drivers.php?mode=license&id=' . $_SESSION['valid_id'] . '">' .
                                sprintf($lang['IncorrectLicense'], $lang['ChampSignupChampionship']) . '</a>';

                    //not entered champ
                    else if($_SESSION['valid_id'] != $this->event['champentered'])
                        $signups = '<a href="championships.php?mode=entrants&id=' . $this->event['c_id'] . '">' .
                                sprintf($lang['NotEntered'], $lang['ChampSignupChampionship']) . '</a>';
                    
                    //can enter
                    else
                        $signups = '<a class="open" href="events.php?mode=entrants&id=' . $this->event['e_id'] . '">' .
                                sprintf($lang['NotEntered'], $lang['IncidentEvent']) . '</a>';


                }

                if(isset($this->event['result_count']) && $this->event['result_count'] > 2)
                    $results = '<a href="events.php?mode=results&id=' . $this->event['e_id'] . '">' . $lang['EventResults'] . '</a>
                                 | <a href="events.php?mode=incidents&id=' . $this->event['e_id'] . '">' . $lang['Incidents'] . '</a>';
                else
                    $results = '';
		
		
		$trackname = $this->event['t_fullTitle'];
                $tracklocation = $this->event['t_location'];
                $information =  $this->event['e_description'] .
                        ($this->event['e_description'] == '' ? '' : '<br/><br/>');

                $dates = '';
                
		foreach ($this->dates as $value)
		{
			$dates .= ($value['d_title'] == $this->event['c_title'] ? '' : $value['d_title'] . ': ') . $value['ed_date'] . '<br>';
		}
		
		include $skin_dir . 'tpl/eventMini.tpl';
	}
	
	function renderOverview()
	{
		global $lang, $skin_dir;

		$logo = '';
		if($this->event['t_logo'] != '')
		{
			$logo .= '<img src="' . $this->event['t_logo'] . '" alt=""><br><br>';
		}
		if($this->event['c_logoURL'] != '')
		{
			$logo .= '<img src="' . $this->event['c_logoURL'] . '" alt=""><br><br>';
		}
		if($this->event['g_logo'] != '')
		{
			$logo .= '<img src="' . $this->event['g_logo'] . '" alt="">';
		}
		
		
		
		$title = '<a href="championships.php?id=' . $this->event['c_id'] . '">' .  
                                    $this->event['c_title'] . ' ' . $this->event['e_title'] . '</a>';
		$subtitle = '<a href="events.php?id=' . $this->event['e_id'] . '">' . $this->event['t_fullTitle'] . '</a>';

		$signUpClass = $this->event['e_openSignUps'] == 1 ? 'Open' : 'Closed' ;
		$signUpText = '<a class="' . $signUpClass. '" href="events.php?mode=entrants&id=' . $_GET['id'] . '">' . $lang['entrants'] . '</a>';
		
		$dates = $this->event['t_location'] == '' ? '' : $this->event['t_location'] . '<br><br>' ;
		$dates = $this->event['e_description'] == '' ? $dates : $dates . formatOutput($this->event['e_description']) . '<br><br>';
		
		
		$signUpText .= $this->event['t_downloadURL'] == '' ? '' : '<br><br><a href="' . $this->event['t_downloadURL'] . '">' . 
									$lang['TrackDownload'] . '</a>';
		
		$signUpText .= $this->event['e_preevent'] == '' ? '' : '<br><br><a href="' . $this->event['e_preevent'] . '">' . 
									$lang['EventPreEvent'] . '</a>';
		$signUpText .= $this->event['e_postevent'] == '' ? '' : '<br><br><a href="' . $this->event['e_postevent'] . '">' . 
									$lang['EventPostEvent'] . '</a>';
		
		$event_title = $lang['ChampionshipMeetingText'];
		
		$event_info = formatOutput($this->event['c_meetingText']);
									
		foreach ($this->dates as $value)
		{
			$password = '';
			if(isset($_SESSION["valid_admin"]) && $_SESSION["valid_admin"] >= 1)
			{
				if($value['ed_serverPassword'] != '')
				{
					$password = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $lang['Password'] . ': ' . $value['ed_serverPassword'];
				}
				else if($this->event['c_serverPassword'] != '')
				{
					$password = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $lang['Password'] . ': ' . $this->event['c_serverPassword'];
				}
			}
			$dates = $dates . $value['d_title'] . ': ' . $value['ed_date'] . '<br> ' . $lang['EventCapacity'] . ': ' . $value['ed_capacity'] . $password . '<br><br>';
		}
		
		include $skin_dir . 'tpl/tableStart.tpl';
		include $skin_dir . 'tpl/eventFull.tpl';
		include $skin_dir . 'tpl/tableEnd.tpl';
	}
	
	function setup_data()
	{
		global $db, $lang;
		include_once('includes/renderInclude.php');
		
		if($this->mode == 'entrants')
		{
			include_once('includes/event_signup.php');
			include_once('includes/division_details.php');
		
			$this->items[] = new renderInclude('tpl/tableStart.tpl');
			$this->items[] = new event_details($this->event, 'MiniSolo');
			$this->items[] = new renderInclude('tpl/tableEnd.tpl');
			
			$this->items[] = new event_signup($this->event);
			
			$qDiv = 'SELECT d_id, d_title, ed_capacity FROM ' . DIVISIONS_TABLE . ', ' . EVENTS_TABLE . ', ' . CHAMPIONSHIPS_TABLE .  
					', ' . EVENTDATES_TABLE . 
					' WHERE d_champid = c_id AND c_id = e_championship AND ed_event = e_id AND ed_division = d_id AND e_id = ' . $this->id . 
					' ORDER BY d_order';
			//echo $qDiv;
			$resdiv = $db->sql_query($qDiv);
			while($div = $db->sql_fetchrow($resdiv)) 		
			{
				//succesfully registered
				
				$q = 'select es_no, u.user_id, pf_drivername as user_drivername, c.c_carNumbers, ca.c_id, ca.c_manufacturer, 
							 ca.c_model, cs_image, cs_imageThmb, cs_entryOwner, (cs_driver = cs_entryOwner) as entryOwner,
							 (SELECT t.t_id FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = es_team LIMIT 0, 1) AS t_id,
						     (SELECT t.t_name FROM ' . TEAMS_TABLE . ' t WHERE t.t_id = es_team LIMIT 0, 1) AS t_name '.
					 'from ' . USERS_TABLE . ' u, ' . EVENTSIGNUPS_TABLE . ' es, ' . CHAMPIONSHIPS_TABLE . ' c, ' . CARS_TABLE . ' ca, ' . 
							   EVENTS_TABLE . ', ' . EVENTDATES_TABLE . ', ' . CHAMPSIGNUPS_TABLE . ', ' . DRIVERNAME_TABLE . ' d '  . 
					'where cs_driver = es_driver AND cs_champ = e_championship AND es_driver = u.user_id AND e_championship = c.c_id AND 
					 	   es_division = ' . $div['d_id']  . ' AND es_event = ' . $this->id . ' AND es_division = ed_division AND 
						   ed_event = e_id AND es_car = ca.c_id AND es_event = e_id AND u.user_id = d.user_id
					 ORDER BY entryOwner DESC, es_signuptime ASC, user_drivername ASC';	

				$count = 0;
				$resultsArray = Array();		
				$indexArray = Array();
				$result = $db->sql_query($q);
				
				while($row = $db->sql_fetchrow($result)) 
				{
					if($row['user_id'] == $row['cs_entryOwner'])
					{
						$row['teammates'] = Array();
						$resultsArray[$count] = $row;
						$indexArray[$row['user_id']] = $count;
						$count++;
					}
					else
					{
						$resultsArray[$indexArray[$row['cs_entryOwner']]]['teammates'][] = $row;
					}
				}
				
				
				$firstRow = true;
				$entCount = 0;
				for($x = 0; $x < sizeof($resultsArray) && $x <  $div['ed_capacity']; $x++)
				{
					$row = $resultsArray[$x];
					if($firstRow)
					{
						$c = $div['ed_capacity'] > $count ? $count : $div['ed_capacity'];
						$this->items[] = new addMessage('', ($div['d_id']  == 0 ? $lang['Unassigned'] : $div['d_title']) . ' (' .$c . 
											($div['d_id']  == 0 ? '' : '/' . $div['ed_capacity']) . ' ' . $lang["entrants"] . ')', true, true);
						$this->items[] = new renderInclude('tpl/tableStart.tpl');
						$firstRow = false;
					}
					if($entCount == 0)
					{
						$this->items[] = new renderInclude('tpl/rowStart.tpl');
					}
					if($entCount == 2)
					{
						$this->items[] = new renderInclude('tpl/rowEnd.tpl');
						$entCount = 0;
					}
					$this->items[] = new entrant($row['es_no'], $row['user_id'], $row['user_drivername'], $row['t_id'], $row['t_name'], 
												$row['c_id'], $row['c_manufacturer'] . ' ' . $row['c_model'], $row['c_carNumbers'], 
												$row['cs_image'], $row['cs_imageThmb'], $row['teammates']);
					$entCount++;
					
				}
				if($entCount == 1)
				{
					$this->items[] = new empty_entrant();
				}
				if(!$firstRow)
				{
					$this->items[] = new renderInclude('tpl/rowEnd.tpl');
					$this->items[] = new renderInclude('tpl/tableEnd.tpl');
				}
				
				$firstRow = true;
				$entCount = 0;
				for($x = $div['ed_capacity']; $x < sizeof($resultsArray); $x++)
				{
					$row = $resultsArray[$x];
					if($firstRow)
					{
						$c = $db->sql_numrows($result);
						$this->items[] = new addMessage('', $lang['Reserves'], true, true);
						$this->items[] = new renderInclude('tpl/tableStart.tpl');
						$firstRow = false;
					}
					if($entCount == 0)
					{
						$this->items[] = new renderInclude('tpl/rowStart.tpl');
					}
					if($entCount == 2)
					{
						$this->items[] = new renderInclude('tpl/rowEnd.tpl');
						$entCount = 0;
					}
					$this->items[] = new entrant($row['es_no'], $row['user_id'], $row['user_drivername'], $row['t_id'], $row['t_name'], 
												$row['c_id'], $row['c_manufacturer'] . ' ' . $row['c_model'], $row['c_carNumbers'], $row['cs_image'], 
												 $row['cs_imageThmb']);
					$entCount++;
					
				}
				if($entCount == 1)
				{
					$this->items[] = new empty_entrant();
				}
				if(!$firstRow)
				{
					$this->items[] = new renderInclude('tpl/rowEnd.tpl');
					$this->items[] = new renderInclude('tpl/tableEnd.tpl');
				}
			}
		}
		else if($this->mode == 'results')
		{
			$this->items[] = new renderInclude('tpl/tableStart.tpl');
			$this->items[] = new event_details($this->event, 'MiniSolo');
			$this->items[] = new renderInclude('tpl/tableEnd.tpl');
			
			include_once('includes/event_results.php');
                        $choice = new event_racechoice($this->event['e_id']);
                        $this->items[] = $choice;

                        if($this->event['c_stagedEvents'] == 0)
                        {
                            $this->items[] = new event_results($this->event['e_id'], $choice->getDivisionID(), $choice->getRaceID(),
							'events.php?mode=results&id=' .$this->event['e_id'] );
                        }
                        else
                        {
                            $this->items[] = new rally_results($this->event['e_id'], $choice->getDivisionID(),
							'events.php?mode=results&id=' .$this->event['e_id'] );
                        }
		}
		else if($this->mode == 'incidents')
		{
			$this->items[] = new renderInclude('tpl/tableStart.tpl');
			$this->items[] = new event_details($this->event, 'MiniSolo');
			$this->items[] = new renderInclude('tpl/tableEnd.tpl');
			
			include_once('includes/incident_public.php');
			$this->items[] = new incident_public($this->id);
		}
		else
		{
			$qD = "select d_title, ed_capacity, ed_serverPassword, date_format(ed_date, '%M %e, %Y, %l:%i%p') as ed_date from " . EVENTDATES_TABLE . ', ' . DIVISIONS_TABLE .
				' where ed_division = d_id AND ed_event = ' . $this->event['e_id'];
			$result = $db->sql_query($qD);
			
			//put dates into local array
			while($row = $db->sql_fetchrow($result)) 
			{
				$this->dates[] = $row;
			}
		}
	}
	
	
	function setup_overview()
	{
		include_once('includes/page_header.php');
		//$this->items[] = new page_header($this->event, 'c_title', 't_fullTitle', 'c_logoThmbURL', 't_logo');
	}


}

?>